package org.egrit;

import java.awt.Point;

import com.vividsolutions.jts.geom.Polygon;

/**
 * A grid polygon iterator that applies a translation to grid positions. 
 * When working with a series of grid regions, all having the same
 * size and shape but different positions, you can create a single polygon
 * and then have a separate translated iterator for each region.
 * 
 * @author michael
 */
public class TranslatedGridPolygonIterator extends GridGeometryIterator {
    private final int dx;
    private final int dy;

    /**
     * Creates a new iterator.
     * 
     * @param poly the reference polygon
     * @param dx x translation
     * @param dy y translation
     */
    public TranslatedGridPolygonIterator(Polygon poly, int dx, int dy) {
        super(poly);
        this.dx = dx;
        this.dy = dy;
    }

    /**
     * Returns the current iterator position with the translation applied.
     * Note that if the target bounds are empty, this will always 
     * return {@code Point(0, 0)}.
     * 
     * @return translated position
     */
    @Override
    public Point getPos() {
        if (isEmpty()) {
            return new Point();
        }
        
        Point p = super.getPos();
        p.translate(dx, dy);
        return p;
    }
    
}
